# Tools
CC = @CCACHE@ @CC@
CXX = @CCACHE@ @CXX@
RANLIB = @RANLIB@
AR = @AR@
STRIP = @STRIP@

# Configuration

SH_CFLAGS ?= @SH_CFLAGS@
SH_LDFLAGS ?= @SH_LDFLAGS@
@if SH_SOPREFIX
SH_LDFLAGS += @SH_SOPREFIX@libjim@LIBSOEXT@
@endif
SHOBJ_CFLAGS ?= @SHOBJ_CFLAGS@
@if JIM_STATICLIB
SHOBJ_LDFLAGS ?= @SHOBJ_LDFLAGS@
@else
SHOBJ_LDFLAGS ?= @SHOBJ_LDFLAGS_R@
@endif
CFLAGS = @CFLAGS@ @CCOPTS@ @CPPFLAGS@
CXXFLAGS = @CXXFLAGS@ @CXXOPTS@
LDFLAGS = @LDFLAGS@
LDLIBS += @LDLIBS@
LIBS += @LIBS@
exec_prefix ?= @exec_prefix@
prefix ?= @prefix@

CC += -D_GNU_SOURCE -Wall $(OPTIM) -I.
CXX += -D_GNU_SOURCE -Wall $(OPTIM) -I.
@if srcdir != .
CFLAGS += -I@srcdir@
CXXFLAGS += -I@srcdir@
VPATH := @srcdir@
@endif

@if JIM_STATICLIB
LIBJIM := libjim.a
@else
LIBJIM := libjim@LIBSOEXT@
SH_LIBJIM := $(LIBJIM)
CC += $(SH_CFLAGS)
CXX += $(SH_CFLAGS)
DEF_LD_PATH := @LD_LIBRARY_PATH@="@builddir@:$(@LD_LIBRARY_PATH@)"
@endif

@if HAVE_CXX_EXTENSIONS
JIMSH_CC := $(CXX) $(CXXFLAGS)
@else
JIMSH_CC := $(CC) $(CFLAGS)
@endif

OBJS := _load-static-exts.o jim-subcmd.o jim-interactive.o jim-format.o jim.o utf8.o jimregexp.o \
    @EXTRA_OBJS@ @C_EXT_OBJS@ @TCL_EXT_OBJS@

JIMSH := jimsh@EXEEXT@

@if JIM_INSTALL
INSTALL_DATA_DIR ?= mkdir -p
INSTALL_DATA ?= cp
INSTALL_PROGRAM ?= cp
@endif

all: $(JIMSH) @C_EXT_SHOBJS@

# Create C extensions from pure Tcl extensions
.SUFFIXES: .tcl
.tcl.o:
	@tclsh@ @srcdir@/make-c-ext.tcl $< >_$*.c
	$(CC) $(CFLAGS) -c -o $@ _$*.c

docs: Tcl.html

@if JIM_DOCS
install-docs: docs
	$(INSTALL_DATA_DIR) $(DESTDIR)$(prefix)/doc/jim
	$(INSTALL_DATA) Tcl.html $(DESTDIR)$(prefix)/doc/jim
@else
install-docs:
@endif

$(JIMSH): $(LIBJIM) jimsh.o initjimsh.o
	$(JIMSH_CC) @SH_LINKFLAGS@ $(LDFLAGS) -o $@ jimsh.o initjimsh.o $(LIBJIM) $(LDLIBS) $(LIBS)

@if JIM_INSTALL
install: all @TCL_EXTS@ install-exec install-docs
	$(INSTALL_DATA_DIR) $(DESTDIR)@libdir@/jim
	$(INSTALL_DATA) $(LIBJIM) $(DESTDIR)@libdir@
	$(INSTALL_DATA) @srcdir@/README.extensions @C_EXT_SHOBJS@ $(DESTDIR)@libdir@/jim
	for i in @TCL_EXTS@; do $(INSTALL_DATA) @srcdir@/$$i $(DESTDIR)@libdir@/jim; done
	$(INSTALL_DATA_DIR) $(DESTDIR)@includedir@
	$(INSTALL_DATA) @srcdir@/jim.h @srcdir@/jim-eventloop.h @srcdir@/jim-signal.h \
		@srcdir@/jim-subcmd.h @srcdir@/jim-win32compat.h $(DESTDIR)@includedir@
	$(INSTALL_DATA) jim-config.h $(DESTDIR)@includedir@
	$(INSTALL_DATA_DIR) $(DESTDIR)@bindir@
	$(INSTALL_DATA) build-jim-ext $(DESTDIR)@bindir@

install-exec: all
	$(INSTALL_DATA_DIR) $(DESTDIR)@bindir@
	$(INSTALL_PROGRAM) $(JIMSH) $(DESTDIR)@bindir@

uninstall:
	rm -f $(DESTDIR)@bindir@/$(JIMSH)
	rm -f $(DESTDIR)@bindir@/build-jim-ext
	rm -f $(DESTDIR)@libdir@/$(LIBJIM)
	for i in README.extensions @C_EXT_SHOBJS@ @TCL_EXTS@; do rm -f $(DESTDIR)@libdir@/jim/$$i; done
	rm -f $(DESTDIR)@includedir@/jim*.h
	rm -f $(DESTDIR)@datadir@/doc/jim/Tcl.html
@else
install install-exec: all
uninstall:
@endif

test check: $(JIMSH)
	cd @srcdir@/tests; $(DEF_LD_PATH) $(MAKE) jimsh=@builddir@/jimsh

$(OBJS): Makefile $(wildcard *.h)

@if JIM_UTF8
# Generate the unicode case mapping
utf8.o: _unicode_mapping.c

_unicode_mapping.c: @srcdir@/UnicodeData.txt @srcdir@/parse-unidata.tcl
	@tclsh@ @srcdir@/parse-unidata.tcl @srcdir@/UnicodeData.txt >$@ || ( rm $@; exit 1)
@endif

_load-static-exts.c: @srcdir@/make-load-static-exts.tcl Makefile
	@tclsh@ @srcdir@/make-load-static-exts.tcl @STATIC_EXTS@ >$@ || ( rm $@; exit 1)

@if JIM_STATICLIB
$(LIBJIM): $(OBJS)
	$(AR) cr $@ $(OBJS)
	$(RANLIB) $@
@else
$(LIBJIM): $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(SH_LDFLAGS) -o $@ $(OBJS) $(LDLIBS) $(LIBS)
@endif

# Note that $> $^ is for compatibility with both GNU make and BSD make
readdir.so: jim-readdir.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-readdir.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-readdir.o $(SH_LIBJIM)

array.so: jim-array.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-array.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-array.o $(SH_LIBJIM)

clock.so: jim-clock.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-clock.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-clock.o $(SH_LIBJIM)

file.so: jim-file.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-file.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-file.o $(SH_LIBJIM)

posix.so: jim-posix.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-posix.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-posix.o $(SH_LIBJIM)

regexp.so: jim-regexp.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-regexp.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-regexp.o $(SH_LIBJIM)

syslog.so: jim-syslog.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-syslog.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-syslog.o $(SH_LIBJIM)

readline.so: jim-readline.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-readline.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-readline.o $(SH_LIBJIM) @LDLIBS_readline@

pack.so: jim-pack.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-pack.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-pack.o $(SH_LIBJIM) @LDLIBS_pack@

tclprefix.so: jim-tclprefix.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-tclprefix.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-tclprefix.o $(SH_LIBJIM) @LDLIBS_tclprefix@

sqlite3.so: jim-sqlite3.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-sqlite3.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-sqlite3.o $(SH_LIBJIM) @LDLIBS_sqlite3@

win32.so: jim-win32.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-win32.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-win32.o $(SH_LIBJIM) @LDLIBS_win32@

mk.so: jim-mk.cpp
	$(CXX) $(CXXFLAGS) $(SHOBJ_CFLAGS) -c -o jim-mk.o $> $^
	$(CXX) $(CXXFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-mk.o $(SH_LIBJIM) @LDLIBS_mk@

sdl.so: jim-sdl.c
	$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-sdl.o $> $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-sdl.o $(SH_LIBJIM) @LDLIBS_sdl@

Tcl.html: jim_tcl.txt
	@tclsh@ @srcdir@/make-index $> $^ | asciidoc -o $@ -d manpage - || cp @srcdir@/Tcl_shipped.html Tcl.html

clean:
	rm -f *.o *.so *.dll *.exe lib*.a $(JIMSH) $(LIBJIM) Tcl.html _*.c

distclean: clean
	rm -f jimautoconf.h jim-config.h Makefile config.log autosetup/jimsh0@EXEEXT@ build-jim-ext

ship: Tcl.html
	cp $< Tcl_shipped.html

# automake compatibility. do nothing for all these targets
EMPTY_AUTOMAKE_TARGETS := dvi pdf ps info html tags ctags mostlyclean maintainer-clean check installcheck installdirs \
 install-pdf install-ps install-info install-html -install-dvi uninstall install-data
.PHONY: $(EMPTY_AUTOMAKE_TARGETS)
$(EMPTY_AUTOMAKE_TARGETS):

# automake compatibility - install sources from the current dir to $(distdir)
distdir_full := $(shell cd $(distdir); pwd)
distdir:
	cd "@srcdir@"; git ls-files | cpio -pdmu $(distdir_full)

reconfig:
	CC='@CC@' @AUTOREMAKE@
